<?php

namespace dcloud;
 
use think\Exception;
use think\facade\Config;
use think\facade\Request;

class CloudFunctions
{

 

    protected $url='';
    protected $secret='';
    public function __construct()
    {

        $config = Config::get('dcloud.');
         $this->url = $config['url'];
         $this->secret = $config['secret'];

       
    }
    public function __call($method, $params)
    {
        $url = $this->url.$method;
        $sign = $this->getSignature($params, $this->secret);
        $requestUrl = $url . "?sign=" . $sign . "&" . http_build_query($params);
        $result = json_decode(file_get_contents($requestUrl), true);
        return $result;
    }
 

    /**
     * 获取签名
     * @param array $arrdata 签名数组
     * @param string $key 密钥
     * @return boolean|string 签名值
     */
    protected function getSignature($arrdata, $key)
    {
        ksort($arrdata);
        $paramstring = "";
        foreach ($arrdata as $key => $value) {
            if (strlen($paramstring) == 0) {
                $paramstring .= $key . "=" . $value;
            } else {
                $paramstring .= "&" . $key . "=" . $value;
            }

        }
        $stringSignTemp = rtrim($paramstring, '&');
        $paySign = hash_hmac('sha256', $stringSignTemp, $key);
        return $paySign;
    }
   
}
